//
// Model for traffic induced by worm.
//
// @author Michael Liljenstam, ISTS, Dartmouth College
//
// $Id: MeanRateEgressTraffic.java,v 1.3 2003/03/08 02:03:55 mili Exp $

package SSF.App.Worm;

import SSF.OS.*;


/**
 *  Mean scan rate model of worm induced scan traffic, global object.
 *  Models the scan traffic as a simple mean rate flow.
 *  The scanning strategy is assumed to be Uniform scanning over the 
 *  whole 2^32 IPv2 address space (e.g. CRv2).
 */
public class MeanRateWormTraffic extends WormTraffic {

    public MeanRateWormTraffic() {
	// create worm epidemic state objects for each AS according to 
	// this implementation
	int numASs = MacroscopicModel.ref.net.numASs;

	for (int i=0; i < numASs; i++) {
	    AS as = MacroscopicModel.ref.net.ases[i];
	    as.wormTraffic = new MeanRateWormTrafficState(as);
	}
    }

    /** Single worm mean scan rate. */
    public float meanScanRate = 4.76F; // default 4.76 probes/s per worm (CR)

    /**
     *  State update.
     */
    public void update() {
	ASGraph asGraph = MacroscopicModel.ref.net;

	// update each AS state with next timestep
	for (int j = 0; j < asGraph.numASs; j++) {
	    asGraph.ases[j].wormTraffic.update();
	}
    }

    /**
     *  Set mean scan rate per worm [scans/second].
     */
    public void setMeanScanRate(float rate) {
	meanScanRate = rate;
	if (MacroscopicModel.ref.DEBUG) {
	    System.err.println("Worm mean scan rate set to " + meanScanRate 
			       + " probes/s");
	}
    }

    /** 
     *  Create a scan packet according to this model, i.e. uniform scanning. 
     */
    public IpHeader makeScanPacket() {
	// sample src ip

	// sample dst ip (use the uniform rng in MacroscopicModel)
	int dstIp = (int) MacroscopicModel.ref.infUniformDistr
	    .nextLongFromTo(0, (long) Math.pow(2.0,32.0));

	// fake the scan packet IP hdr --- FIXME
	IpHeader scanIp = 
	    new IpHeader(Protocols.TCP_PRTL_NUM, 
			 0x1, // src ip
			 dstIp, // dst ip
			 32); // ttl

	return scanIp;
    }

} // class WormTraffic


